<?php
class Event extends AppModel {		
	var $name = 'Event';
	
	// Define the join relationship between family and event
	var $hasAndBelongsToMany = array('Family');
	
	/**
	 * Get the event information from the event ID
	 */
	function getEventFromID($eid) {
		$result = $this->find('first', array('conditions' => array('Event.id' => $eid)));
		return $result['Event'];
	}
				
	/*
	 * Gets the next page number for an event view as a function of pictures per
	 * page.
	 *
	 * $numPictures: Number of pictures in event
	 * $currentPage: The current page number. Must be a valid page number in the
	 * allowed range.
	 * $picsPerPage: The number of pictures being displayed per page.
	 *
	 * return: The integer number of the next page
	 */
	function getNextPage($numPictures, $currentPage, $picsPerPage) {
		if ($numPictures == 0 || $currentPage < 1) {
			return 1;
		}

		$totalPages = ceil($numPictures / $picsPerPage);
		$nextPage = $currentPage + 1;

		return ($nextPage > $totalPages) ? $totalPages : $nextPage;
	}

	/**
	 * Function used to delete directories from the filesystem. Code taken from:
	 *					http://php.net/manual/en/function.rmdir.php
	 */
	function rrmdir($dir) {
		if (is_dir($dir)) {
			$objects = scandir($dir);
			foreach ($objects as $object) {
				if ($object != "." && $object != "..") {
					if (filetype($dir."/".$object) == "dir") rrmdir($dir."/".$object); else unlink($dir."/".$object);
				}
			}
			reset($objects);
			rmdir($dir);
		}
	} 
	
	/**
	 * Returns the names of the families who are part of the given event.
	 */
	function getEventFamilyNames($eid){
		$res = $this->find('first', array('conditions' => array('id' => $eid)));
		$names = array();
						
		foreach($res['Family'] as $fid => $val){
			array_push(&$names, $val['name']);
		}
						
		return $names;
	}

	/*
	 * Returns the ids of the families who are part of the given event.
	 */
	function getEventFamilyIDs($eid){
		$res = $this->find('first', array('conditions' => array('id' => $eid)));
								
		$names = array();

		foreach ($res['Family'] as $fid => $val) {
			array_push(&$names, $val['id']);
		}

		return $names;
	}

	/*
	 * Gets the previous page number for an event view as a function of pictures per page.
	 *
	 * $numPictures: Number of pictures in event
	 * $currentPage: The current page number. Must be a valid page number in the allowed range.
	 * $picsPerPage: The number of pictures being displayed per page.
	 *
	 * return: The integer number of the previous page
	 */				 
	function getPreviousPage($numPictures, $currentPage, $picsPerPage) {
		if ($numPictures == 0) {
			return 1;
		}
				
		$totalPages = ceil($numPictures / $picsPerPage);
		$prevPage = $currentPage - 1;
						
		if ($currentPage > $totalPages) {
			return $totalPages;
		}
						
		return ($prevPage < 1) ? 1 : $prevPage;
	}
				
	/*
	 * Returns the id of the family hosting the event with the given id.
	 */
	function getHostFromID($eid){
		$result = $this->find('first', array('conditions' => array('Event.id' => $eid),
																				 'fields' => array('Event.family_id')));
		return $result['Event']['family_id'];
	}
				
	/**
	 * Provides validation for an event creation
	 */
	var $validate = array('name' => array(
							'name-lengthRule' => array(
								'rule' => array('between', 1, 100),
								'last' => true,
								'required' => true,
								'allowempty' => false,
								'message' => 'Event name must be between 1 and 100 characters! '		  	                          ),
							'name-regexRule' => array(
								'rule' => '/^[^\\\\\/]*$/i',
								'message' => 'Name must not contain / or \\'
							)
						 ),
												'eventdate' => array(
																						 'rule' => 'date',
																						 'message' => 'Please enter a valid date'
																						 )
												);
	}
?>
